package com.shop2cn.iapi.couponmanage.testcase.old;

import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.couponmanage.parameter.CreateBagBean;
import com.shop2cn.iapi.couponmanage.parameter.CreateCouponBean;
import com.shop2cn.iapi.couponmanage.parameter.QuerySendTask;
import com.shop2cn.iapi.couponmanage.parameter.QuerySendTaskBean;
import com.shop2cn.iapi.couponmanage.parameter.TaskInfo;
import com.shop2cn.iapi.couponmanage.parameter.createTaskByUserIdBean;
import com.shop2cn.iapi.couponmanage.service.CreateBagCall;
import com.shop2cn.iapi.couponmanage.service.CreateCouponCall;
import com.shop2cn.iapi.couponmanage.service.QuerySendTaskCall;
import com.shop2cn.iapi.couponmanage.service.createTaskByUserIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponShop2cnMCallService;
import com.ymttest.database.model.couponbag;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.model.couponsendtask;
import com.ymttest.database.sqlwapper.couponbagWapper;
import com.ymttest.database.sqlwapper.couponbatchWapper;
import com.ymttest.database.sqlwapper.couponsendtaskWapper;
//import com.ymttest.database.model.couponbatch;
//import com.ymttest.database.model.couponsendtask;
//import com.ymttest.database.sqlwapper.couponbatchWapper;
//import com.ymttest.database.sqlwapper.couponsendtaskWapper;
import com.ymttest.utils.YMTDateUtil;

/**
 * 查询发放任务用例 FreeMaker Template自动生成代码
 * 这个接口查询的平台创建的链接发放优惠券任务
 */
@TestSuite
public class Ts_QuerySendTask {
	private static QuerySendTaskBean querysendtaskBean;
	private static QuerySendTaskCall querysendtaskCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询发放任务用例");
	}

	@Before
	public void caseUp() {
		querysendtaskBean = new QuerySendTaskBean();
		querysendtaskCall = new QuerySendTaskCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(querysendtaskCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QuerySendTask_001() {
		Logger.start(true, "正例-查询发放任务-单条SubTask");
		try {
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			createTaskByUserIdBean createTaskByUserIdBean=couponCallService.setCreateTaskByUserIdBeanForPlatform();
			
			List<Integer> userIds=new ArrayList<Integer>();
			for(int i=1;i<10;i++)
			{
			  userIds.add(i);
			}
			createTaskByUserIdBean.setUserIds(userIds);
			couponCallService.createTaskByUserId(createTaskByUserIdBean);
			
			couponsendtask task = new couponsendtaskWapper().selectTop1().get(0);
			couponbatch batch = new couponbatchWapper().selectBybatchCode(task.getBatchCode()).get(0);
			
			querysendtaskBean.setBatchCode(createTaskByUserIdBean.getBatchCode());
			Date date1 = new Date(task.getCreateTime().getTime() - 2 * 60 * 60 * 1000);
			Date date2 = new Date(task.getCreateTime().getTime() + 2 * 60 * 60 * 1000);
			querysendtaskBean.setCreateStartTime(YMTDateUtil.getFormatTime(date1, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setCreateEndTime(YMTDateUtil.getFormatTime(date2, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setPageIndex(1);
			querysendtaskBean.setPageSize(1);
			querysendtaskCall.setData(querysendtaskBean);
			querysendtaskCall.callService();

			verifyReturn(task,batch);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QuerySendTask_002() {
		Logger.start(true, "正例-查询发放任务-多条SubTask");
		try {
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			createTaskByUserIdBean createTaskByUserIdBean=couponCallService.setCreateTaskByUserIdBeanForPlatform();
			
			List<Integer> userIds=new ArrayList<Integer>();
			for(int i=1;i<105;i++)//目前设置每个task最多100个发放用户
			{
			  userIds.add(i);
			}
			createTaskByUserIdBean.setUserIds(userIds);
			couponCallService.createTaskByUserId(createTaskByUserIdBean);
			
			couponsendtask task = new couponsendtaskWapper().selectTop1().get(0);
			couponbatch batch = new couponbatchWapper().selectBybatchCode(task.getBatchCode()).get(0);
			
			querysendtaskBean.setBatchCode(createTaskByUserIdBean.getBatchCode());
			Date date1 = new Date(task.getCreateTime().getTime() - 2 * 60 * 60 * 1000);
			Date date2 = new Date(task.getCreateTime().getTime() + 2 * 60 * 60 * 1000);
			querysendtaskBean.setCreateStartTime(YMTDateUtil.getFormatTime(date1, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setCreateEndTime(YMTDateUtil.getFormatTime(date2, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setPageIndex(1);
			querysendtaskBean.setPageSize(1);
			querysendtaskCall.setData(querysendtaskBean);
			querysendtaskCall.callService();

			verifyReturn(task,batch);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QuerySendTask_003() {
		Logger.start(false, "正例-BatchCode空-返回除兑换码之外的所有数据(若是套券，则batchId=0)");
		try {
			couponsendtask task1 = new couponsendtaskWapper().selectTop1().get(0);
			//couponbatch batch1 = new couponbatchWapper().selectBybatchCode(task1.getBatchCode()).get(0);
			Date date1 = new Date(task1.getCreateTime().getTime() - 2 * 60 * 60 * 1000);
			Date date2 = new Date(task1.getCreateTime().getTime() + 2 * 60 * 60 * 1000);
			String stratTime=YMTDateUtil.getFormatTime(date1, "yyyy-MM-dd'T'HH:mm:ss");
			String endTime=YMTDateUtil.getFormatTime(date2, "yyyy-MM-dd'T'HH:mm:ss");

			querysendtaskBean.setBatchCode("");
			querysendtaskBean.setCreateStartTime(stratTime);
			querysendtaskBean.setCreateEndTime(endTime);
			querysendtaskBean.setPageIndex(1);
			querysendtaskBean.setPageSize(10);
			querysendtaskCall.setData(querysendtaskBean);
			querysendtaskCall.callService();
			
			List<couponsendtask> DBtasks = new couponsendtaskWapper().
					selectByCreateTimeAndTaskType(stratTime,endTime);
			
			Logger.verifyEquals("0", querysendtaskCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", querysendtaskCall.getMessage(), "验证返回Message");
			QuerySendTask tasks = querysendtaskCall.getQuerySendTask();

			Logger.verifyEquals(DBtasks.size(), tasks.getRecordCount(), "验证返回RecordCount");
			List<TaskInfo> infos = tasks.getTaskList();
			
		   int taskSize=DBtasks.size()>querysendtaskBean.getPageSize()?querysendtaskBean.getPageSize():DBtasks.size();
		   for(int i=0;i<taskSize;i++){
		    	couponsendtask task =DBtasks.get(i);
		    	TaskInfo info=infos.get(i);
		    	int count=i+1;
		    	//Logger.comment("batchCode:"+task.getBatchCode());
		    	couponbatch batch=new couponbatch();
		    	if(task.getBatchCode().contains("B"))
		    	{
		    		batch=new couponbatchWapper().getCouponBatchByBagCode(task.getBatchCode()).get(0);
		    		couponbag bag=new couponbagWapper().selectByPrimaryKey(task.getBatchCode());
					Logger.verifyEquals(0, info.getBatchId(), "验证返回第"+count+"条BatchId");
		    		Logger.verifyEquals(batch.getBagCode(), info.getBatchCode(), "验证返回第"+count+"条BatchCode");
		    		Logger.verifyEquals(bag.getName(), info.getBatchName(), "验证返回第"+count+"条CouponName");
		    	}
		    	else
		    	{
		    	   batch = new couponbatchWapper().selectBybatchCode(task.getBatchCode()).get(0);
					Logger.verifyEquals(batch.getBatchId(), info.getBatchId(), "验证返回第"+count+"条BatchId");
		    	   Logger.verifyEquals(batch.getBatchCode(), info.getBatchCode(), "验证返回第"+count+"条BatchCode");
		    	   Logger.verifyEquals(batch.getCouponName(), info.getBatchName(), "验证返回第"+count+"条CouponName");
		    	}
				String createTime=YMTDateUtil.getFormatTime(task.getCreateTime(), "yyyy-MM-dd'T'HH:mm:ss");
				Logger.verifyEquals(task.getTaskId(), info.getTaskId(), "验证返回第"+count+"条TaskId");
				Logger.verifyEquals(createTime, info.getCreateTime(), "验证返回第"+count+"条CreateTime");
				Logger.verifyEquals(task.getCreaterId(), info.getCreaterId(), "验证返回第"+count+"条CreaterId");
				Logger.verifyEquals(task.getCreaterName(), info.getCreaterName(), "验证返回第"+count+"条CreaterName");
				Logger.verifyEquals(task.getStatus().intValue(), info.getTaskStatus(), "验证返回第"+count+"条Status");
				Logger.verifyEquals(task.getSuccessNum(), info.getCouponSuccessCount(), "验证返回第"+count+"条SuccessNum");
				Logger.verifyEquals(task.getFailNum(), info.getCouponFailCount(), "验证返回第"+count+"条FailNum");
				Logger.verifyEquals(task.getTotalNum(), info.getCouponTotal(), "验证返回第"+count+"条TotalNum");
				Logger.verifyEquals(task.getBatchCode(), info.getBatchCode(), "验证数据库batchCode与返回第"+count+"条BatchCode一致");
				Logger.verifyEquals(task.getTaskId(), info.getTaskId(), "验证返回第"+count+"条taskId");
		    }
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QuerySendTask_004() {
		Logger.start(true, "反例-BatchCode不存在");
		try {
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			createTaskByUserIdBean createTaskByUserIdBean=couponCallService.setCreateTaskByUserIdBeanForPlatform();
			
			List<Integer> userIds=new ArrayList<Integer>();
			userIds.add(11);

			createTaskByUserIdBean.setUserIds(userIds);
			couponCallService.createTaskByUserId(createTaskByUserIdBean);
			
			couponsendtask task = new couponsendtaskWapper().selectTop1().get(0);
			couponbatch batch = new couponbatchWapper().selectBybatchCode(task.getBatchCode()).get(0);
			
			querysendtaskBean.setBatchCode(createTaskByUserIdBean.getBatchCode()+1);
			Date date1 = new Date(task.getCreateTime().getTime() - 2 * 60 * 60 * 1000);
			Date date2 = new Date(task.getCreateTime().getTime() + 2 * 60 * 60 * 1000);
			querysendtaskBean.setCreateStartTime(YMTDateUtil.getFormatTime(date1, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setCreateEndTime(YMTDateUtil.getFormatTime(date2, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setPageIndex(1);
			querysendtaskBean.setPageSize(10);
			querysendtaskCall.setData(querysendtaskBean);
			querysendtaskCall.callService();

			QuerySendTask tasks = querysendtaskCall.getQuerySendTask();
			Logger.verifyEquals("0", querysendtaskCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", querysendtaskCall.getMessage(), "验证返回Message");
			Logger.verifyEquals(0, tasks.getRecordCount(), "验证返回recordCount数量");
			Logger.verifyEquals(0, tasks.getTaskList().size(), "验证返回taskList集合数量");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QuerySendTask_005() {
		Logger.start(true, "反例-时间区间不正确查不出数据");
		try {
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			createTaskByUserIdBean createTaskByUserIdBean=couponCallService.setCreateTaskByUserIdBeanForPlatform();
			
			List<Integer> userIds=new ArrayList<Integer>();
			userIds.add(11);

			createTaskByUserIdBean.setUserIds(userIds);
			couponCallService.createTaskByUserId(createTaskByUserIdBean);
			
			couponsendtask task = new couponsendtaskWapper().selectTop1().get(0);
			couponbatch batch = new couponbatchWapper().selectBybatchCode(task.getBatchCode()).get(0);
			
			querysendtaskBean.setBatchCode(createTaskByUserIdBean.getBatchCode());
			Date date1 = new Date(task.getCreateTime().getTime() - 4 * 60 * 60 * 1000);
			Date date2 = new Date(task.getCreateTime().getTime() - 2 * 60 * 60 * 1000);
			querysendtaskBean.setCreateStartTime(YMTDateUtil.getFormatTime(date1, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setCreateEndTime(YMTDateUtil.getFormatTime(date2, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setPageIndex(1);
			querysendtaskBean.setPageSize(10);
			querysendtaskCall.setData(querysendtaskBean);
			querysendtaskCall.callService();

			QuerySendTask tasks = querysendtaskCall.getQuerySendTask();
			Logger.verifyEquals("0", querysendtaskCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", querysendtaskCall.getMessage(), "验证返回Message");
			Logger.verifyEquals(0, tasks.getRecordCount(), "验证返回recordCount数量");
			Logger.verifyEquals(0, tasks.getTaskList().size(), "验证返回taskList集合数量");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QuerySendTask_006() {
		Logger.start(true, "反例-时间区间CreateStartTime>CreateEndTime不返回数据");
		try {
			CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
			createTaskByUserIdBean createTaskByUserIdBean=couponCallService.setCreateTaskByUserIdBeanForPlatform();
			
			List<Integer> userIds=new ArrayList<Integer>();
			userIds.add(11);

			createTaskByUserIdBean.setUserIds(userIds);
			couponCallService.createTaskByUserId(createTaskByUserIdBean);
			
			couponsendtask task = new couponsendtaskWapper().selectTop1().get(0);
			couponbatch batch = new couponbatchWapper().selectBybatchCode(task.getBatchCode()).get(0);
			
			querysendtaskBean.setBatchCode(createTaskByUserIdBean.getBatchCode());
			Date date1 = new Date(task.getCreateTime().getTime() - 2 * 60 * 60 * 1000);
			Date date2 = new Date(task.getCreateTime().getTime() - 4 * 60 * 60 * 1000);
			querysendtaskBean.setCreateStartTime(YMTDateUtil.getFormatTime(date1, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setCreateEndTime(YMTDateUtil.getFormatTime(date2, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setPageIndex(1);
			querysendtaskBean.setPageSize(10);
			querysendtaskCall.setData(querysendtaskBean);
			querysendtaskCall.callService();

			QuerySendTask tasks = querysendtaskCall.getQuerySendTask();
			Logger.verifyEquals("0", querysendtaskCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", querysendtaskCall.getMessage(), "验证返回Message");
			Logger.verifyEquals(0, tasks.getRecordCount(), "验证返回recordCount数量");
			Logger.verifyEquals(0, tasks.getTaskList().size(), "验证返回taskList集合数量");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QuerySendTask_007() {
		Logger.start(true, "正例-查询发放任务-单条SubTask-优惠券是套券");
		try {
			String batchCode=callBag();
			
			couponsendtask task = new couponsendtaskWapper().selectTop1().get(0);
			couponbag bag = new couponbagWapper().selectByPrimaryKey(task.getBatchCode());
			
			querysendtaskBean.setBatchCode(batchCode);
			Date date1 = new Date(task.getCreateTime().getTime() - 2 * 60 * 60 * 1000);
			Date date2 = new Date(task.getCreateTime().getTime() + 2 * 60 * 60 * 1000);
			querysendtaskBean.setCreateStartTime(YMTDateUtil.getFormatTime(date1, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setCreateEndTime(YMTDateUtil.getFormatTime(date2, "yyyy-MM-dd'T'HH:mm:ss"));
			querysendtaskBean.setPageIndex(1);
			querysendtaskBean.setPageSize(1);
			querysendtaskCall.setData(querysendtaskBean);
			querysendtaskCall.callService();

			Logger.verifyEquals("0", querysendtaskCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", querysendtaskCall.getMessage(), "验证返回Message");
			QuerySendTask tasks = querysendtaskCall.getQuerySendTask();
			Logger.verifyEquals(1, tasks.getRecordCount(), "验证返回RecordCount");
			List<TaskInfo> infos = tasks.getTaskList();
			TaskInfo info=infos.get(0);
		
			String createTime=YMTDateUtil.getFormatTime(task.getCreateTime(), "yyyy-MM-dd'T'HH:mm:ss");
			Logger.verifyEquals(task.getTaskId(), info.getTaskId(), "验证返回code");
			Logger.verifyEquals(createTime, info.getCreateTime(), "验证返回CreateTime");
			Logger.verifyEquals(0, info.getBatchId(), "验证返回BatchId");
			Logger.verifyEquals(task.getCreaterId(), info.getCreaterId(), "验证返回CreaterId");
			Logger.verifyEquals(task.getCreaterName(), info.getCreaterName(), "验证返回CreaterName");
			Logger.verifyEquals(task.getStatus().intValue(), info.getTaskStatus(), "验证返回Status");
			Logger.verifyEquals(task.getSuccessNum(), info.getCouponSuccessCount(), "验证返回SuccessNum");
			Logger.verifyEquals(task.getFailNum(), info.getCouponFailCount(), "验证返回FailNum");
			Logger.verifyEquals(task.getTotalNum(), info.getCouponTotal(), "验证返回TotalNum");
			Logger.verifyEquals(bag.getBagCode(), info.getBatchCode(), "验证返回BatchCode");
			Logger.verifyEquals(task.getBatchCode(), info.getBatchCode(), "验证数据库batchCode与返回BatchCode一致");
			Logger.verifyEquals(bag.getName(), info.getBatchName(), "验证返回CouponName");
			Logger.verifyEquals(task.getTaskId(), info.getTaskId(), "验证返回taskId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	//创建套券
	private String  callBag() throws ClientProtocolException, IOException
	{
		createTaskByUserIdBean createTaskByUserIdBean=new createTaskByUserIdBean();
		CouponShop2cnMCallService couponCallService = new CouponShop2cnMCallService();
		CreateBagBean createBagBean = couponCallService.setDefaultPlatformCreateCouponBagBeanForRecieve();
		createBagBean.setTotalNum(10000);
		CreateBagCall createBagCall = couponCallService.createPlatformCouponBag(createBagBean);
		
		List<Integer> userIds=new ArrayList<Integer>();
		userIds.add(11);
		createTaskByUserIdBean.setUserIds(userIds);
		createTaskByUserIdBean.setBatchType(2);
		createTaskByUserIdBean.setBatchCode(createBagCall.getbagCode());
		createTaskByUserIdBean.setCreaterId(22);
		createTaskByUserIdBean.setCreaterName("autoTest");
		createTaskByUserIdBean.setNotify(false);
		createTaskByUserIdCall call = new createTaskByUserIdCall();
		call.setData(createTaskByUserIdBean);
		call.callService();
		return createBagCall.getbagCode();
	}
	private void verifyReturn(couponsendtask task, couponbatch batch)
	{
		Logger.verifyEquals("0", querysendtaskCall.getCode(), "验证返回code");
		Logger.verifyEquals("操作成功！", querysendtaskCall.getMessage(), "验证返回Message");
		QuerySendTask tasks = querysendtaskCall.getQuerySendTask();
		Logger.verifyEquals(1, tasks.getRecordCount(), "验证返回RecordCount");
		List<TaskInfo> infos = tasks.getTaskList();
		TaskInfo info=infos.get(0);
	
		String createTime=YMTDateUtil.getFormatTime(task.getCreateTime(), "yyyy-MM-dd'T'HH:mm:ss");
		Logger.verifyEquals(task.getTaskId(), info.getTaskId(), "验证返回code");
		Logger.verifyEquals(createTime, info.getCreateTime(), "验证返回CreateTime");
		Logger.verifyEquals(batch.getBatchId(), info.getBatchId(), "验证返回BatchId");
		Logger.verifyEquals(task.getCreaterId(), info.getCreaterId(), "验证返回CreaterId");
		Logger.verifyEquals(task.getCreaterName(), info.getCreaterName(), "验证返回CreaterName");
		Logger.verifyEquals(task.getStatus().intValue(), info.getTaskStatus(), "验证返回Status");
		Logger.verifyEquals(task.getSuccessNum(), info.getCouponSuccessCount(), "验证返回SuccessNum");
		Logger.verifyEquals(task.getFailNum(), info.getCouponFailCount(), "验证返回FailNum");
		Logger.verifyEquals(task.getTotalNum(), info.getCouponTotal(), "验证返回TotalNum");
		Logger.verifyEquals(batch.getBatchCode(), info.getBatchCode(), "验证返回BatchCode");
		Logger.verifyEquals(task.getBatchCode(), info.getBatchCode(), "验证数据库batchCode与返回BatchCode一致");
		Logger.verifyEquals(batch.getCouponName(), info.getBatchName(), "验证返回CouponName");
		Logger.verifyEquals(task.getTaskId(), info.getTaskId(), "验证返回taskId");
	}
}